declare let $: any;
import { Component, OnInit, } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { Platform, ToastController, NavController, Events } from '@ionic/angular';

import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { Httputils } from '../httputiles';
import { FormBuilder, Validators } from '@angular/forms';


@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss'],
})
export class LoginPage
  implements OnInit {

  mobile: any;
  password: any;
  errorMessage: any;
  sideMenuDisabled = true;
  backButtonPressed: boolean = false; //用于判断返回键是否触发
  url;
  userid: any;
  isChecked = true;
  name: any;
  message: string;
  public userId: number = 0;
  public storeId: number = 0;

  loginForm = this.fb.group({
    username: ['', Validators.required],
    password: ['', Validators.required],

  });

  constructor(public router: Router,
    public rest: Httputils,
    private platform: Platform,
    public toastCtrl: ToastController,
    private splashScreen: SplashScreen,
    private events: Events,
    public nav: NavController,
    private fb: FormBuilder,
  ) {

  }
  ngAfterViewInit() {
    this.splashScreen.hide();
  }
  ngOnInit() {
    this.splashScreen.hide();


  }


  onLogin() {
    console.log(this.loginForm.value)
  }
  login() {
    if (this.mobile === '' || this.mobile === undefined) {
      alert('用户名不能为空')
      return;
    }
    if (this.password === '' || this.password === undefined) {
      alert('请输入密码')
      return;
    }
    this.loginet(this.mobile, this.password);
    // this.jumppage();
  }
  private loginet(mobile, password) {
    var loc = this;
    this.rest.login(mobile, password)
      .subscribe(f => {

        alert(f['message']);
        debugger
        console.log(f['status']);
        if (f['status'] === '1') {
          console.log(f['data']);
          // this.name = data["person"];
          //处理登录成功的页面跳转
          //你也可以存储接口返回的 token（用户是否真实 app=>安全协议）
          // loc.nativeStorage.setItem('UserId', data["id"]);
          // loc.nativeStorage.setItem('mobile', data["account"]);
          // if (this.isChecked) {
          //   loc.nativeStorage.setItem('password', password);
          // } else {
          //   loc.nativeStorage.setItem('password', '');
          // }
          // loc.userid = data["id"];
          // debugger
          // this.loadingCtrl.dismiss();
          // this.events.publish('userinfo', { userid: data["id"], username: mobile, name: this.name, pwd: password }); //侧边栏用户名
          // this.jumppage();
        }
        else {
          // alert( "登录失败");
        }
      }, error =>
          // this.errorMessage = <any>error
          alert("网络连接失败"));
  }


}
